<?php

if (!defined('BASEPATH'))
    exit('Tidak Diperkenankan mengakses langsung');
/* Class  Control : state  * di Buat oleh Diar PHP Generator  *  By Diar */

class ctrstate extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    function index($xAwal = 0, $xSearch = '') {
//  $this->load->view('test.php');
        $idpegawai = $this->session->userdata('idpegawai');
        if (empty($idpegawai)) {
            redirect(site_url(), '');
        }
        if ($xAwal <= -1) {
            $xAwal = 0;
        } $this->session->set_userdata('awal', $xAwal);
        $this->createformstate('0', $xAwal);
    }

    function createformstate($xidx, $xAwal = 0, $xSearch = '') {
        $this->load->helper('form');
        $this->load->helper('html');
        $this->load->model('modelgetmenu');
        $xAddJs = '<script language="javascript" type="text/javascript" src="' . base_url() . 'resource/ajax/baseurl.js"></script>' .
                '<script language="javascript" type="text/javascript" src="' . base_url() . 'resource/ajax/ajaxstate.js"></script>';
        echo $this->modelgetmenu->SetViewAdmin($this->setDetailFormstate($xidx), '', '', $xAddJs, '');
    }

    function setDetailFormstate($xidx) {
        $this->load->helper('form');
        $this->load->model('modelstate');
        $xBufResult = '';
        $xBufResult = '<div id="stylized" class="myform"><h3>state</h3><div class="garis"></div>' . form_open_multipart('ctrstate/inserttable', array('id' => 'form', 'name' => 'form'));
        
        $this->load->helper('common');
        $this->load->model('modelcountry');
        $xBufResult .= '<input type="hidden" name="edidx" id="edidx" value="0" />';
        $xBufResult .= setForm('ediddepartemen', 'Country', form_dropdown('edidcountry', $this->modelcountry->getArrayListcountry(), '', ' id="edidcountry" onChange="dosearchstate(\'0\');" class="require" style="width:200px;" ')) . '<div class="spacer"></div>';
                
//        $xBufResult .= setForm('edidcountry', 'idcountry', form_input(getArrayObj('edidcountry', '', '100'))) . '<div class="spacer"></div>';
        $xBufResult .= setForm('edstate', 'State', form_input(getArrayObj('edstate', '', '200'))) . '<div class="spacer"></div>';
        $xBufResult .= setForm('edcode', 'code', form_input(getArrayObj('edcode', '', '100'))) . '<div class="spacer"></div>';
//        $xBufResult .= setForm('eddatein', 'datein', form_input(getArrayObj('eddatein', '', '100'))) . '<div class="spacer"></div>';
//        $xBufResult .= setForm('edidpegawai', 'idpegawai', form_input(getArrayObj('edidpegawai', '', '100'))) . '<div class="spacer"></div>';
        $xBufResult .= '<div class="garis"></div>' . form_button('btSimpan', 'simpan', 'onclick="dosimpanstate();"') . form_button('btNew', 'new', 'onclick="doClearstate();"') . '<div class="spacer"></div><div id="tabledatastate">' . $this->getliststate(0,'', '') . '</div><div class="spacer"></div>';
        return $xBufResult;
    }

    function getliststate($xAwal, $xidcountry, $xSearch) {
        $xLimit = 10;
        $this->load->helper('form');
        $this->load->helper('common');
        $xbufResult = tbaddrow(tbaddcell('No', '', 'width=10%') .
//                tbaddcell('idcountry', '', 'width=10%') .
                tbaddcell('State', '', 'width=40%') .
                tbaddcell('Code', '', 'width=30%') .
//                tbaddcell('datein', '', 'width=10%') .
//                tbaddcell('idpegawai', '', 'width=10%') .
                tbaddcell('Edit/Hapus', 'padding:5px;', 'width:10%;text-align:center;'), '', TRUE);
        $this->load->model('modelstate');
       $ino = 1+$xAwal; 
        $xQuery = $this->modelstate->getListstatebyxountry($xAwal, $xLimit,$xidcountry,$xSearch);
        foreach ($xQuery->result() as $row) {
            $xButtonEdit = '<img src="' . base_url() . 'resource/imgbtn/edit.png" alt="Edit Data" onclick = "doeditstate(\'' . $row->idx . '\');" style="border:none;width:20px"/>';
            $xButtonHapus = '<img src="' . base_url() . 'resource/imgbtn/delete_table.png" alt="Hapus Data" onclick = "dohapusstate(\'' . $row->idx . '\',\'' . substr($row->idcountry, 0, 20) . '\');" style="border:none;">';
            $xbufResult .= tbaddrow(tbaddcell($ino++) .
//                    tbaddcell($row->idcountry) .
                    tbaddcell($row->state) .
                    tbaddcell($row->code) .
//                    tbaddcell($row->datein) .
//                    tbaddcell($row->idpegawai) .
                    tbaddcell($xButtonEdit . '&nbsp/&nbsp' . $xButtonHapus));
        }
        $xInput = form_input(getArrayObj('edSearch', '', '200'));
        $xButtonSearch = '<img src="' . base_url() . 'resource/imgbtn/b_view.png" alt="Search Data" onclick = "dosearchstate(0);" style="border:none;width:30px;height:30px;" />';
        $xButtonPrev = '<img src="' . base_url() . 'resource/imgbtn/b_prevpage.png" style="border:none;width:20px;" onclick = "dosearchstate(' . ($xAwal - $xLimit) . ');"/>';
        $xButtonNext = '<img src="' . base_url() . 'resource/imgbtn/b_nextpage.png" style="border:none;width:20px;" onclick = "dosearchstate(' . ($xAwal + $xLimit) . ');" />';
        $xbufResult .= tbaddrow(tbaddcell($xInput . $xButtonSearch, '', 'width=10% colspan=2') .
                tbaddcell($xButtonPrev . '&nbsp&nbsp' . $xButtonNext, '', 'width=40% colspan =10'), '', TRUE);
        $xbufResult = tablegrid($xbufResult);
        return '<div class="tabledata"  style="width:100%;left:-12px;">' . $xbufResult . '</div>';
    }

    function editrecstate() {
        $xIdEdit = $_POST['edidx'];
        $this->load->model('modelstate');
        $row = $this->modelstate->getDetailstate($xIdEdit);
        $this->load->helper('json');
        $this->json_data['idx'] = $row->idx;
        $this->json_data['idcountry'] = $row->idcountry;
        $this->json_data['state'] = $row->state;
        $this->json_data['code'] = $row->code;
        $this->json_data['datein'] = $row->datein;
        $this->json_data['idpegawai'] = $row->idpegawai;
        echo json_encode($this->json_data);
    }

    function deletetablestate() {
        $edidx = $_POST['edidx'];
        $this->load->model('modelstate');
        $this->modelstate->setDeletestate($edidx);
        $this->load->helper('json');
        echo json_encode(null);
    }

    function searchstate() {
        $xAwal = $_POST['xAwal'];
        $xSearch = $_POST['xSearch'];
        $xidcountry= $_POST['idcountry'];
        $this->load->helper('json');
        if (($xAwal + 0) == -99) {
            $xAwal = $this->session->userdata('awal', $xAwal);
        }
        if ($xAwal + 0 <= -1) {
            $xAwal = 0;
            $this->session->set_userdata('awal', $xAwal);
        } else {
            $this->session->set_userdata('awal', $xAwal);
        }
        $this->json_data['tabledatastate'] = $this->getliststate($xAwal, $xidcountry, $xSearch);
        echo json_encode($this->json_data);
    }

    function simpanstate() {
        $this->load->helper('json');
        if (!empty($_POST['edidx'])) {
            $xidx = $_POST['edidx'];
        } else {
            $xidx = '0';
        }
        $xidcountry = $_POST['edidcountry'];
        $xstate = $_POST['edstate'];
        $xcode = $_POST['edcode'];
        $xdatein = $_POST['eddatein'];
        $xidpegawai = $_POST['edidpegawai'];
        $this->load->model('modelstate');
        $idpegawai = $this->session->userdata('idpegawai');
        if (!empty($idpegawai)) {
            if ($xidx != '0') {
                $xStr = $this->modelstate->setUpdatestate($xidx, $xidcountry, $xstate, $xcode, $xdatein, $idpegawai);
            } else {
                $xStr = $this->modelstate->setInsertstate($xidx, $xidcountry, $xstate, $xcode, $xdatein, $idpegawai);
            }
        }
        echo json_encode(null);
    }

}

?>